<?php
if(!$_COOKIE["q_session"]){
	setcookie("q_session", md5(date("Y-m-d H:i:s")));
}
if(!$_COOKIE["f_session"]){
	setcookie("f_session", md5(date("Y-m")));
}
session_start();
include_once( 'config.php' );
include_once( 'saetv2.ex.class.php' );
$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );

$uidGet = $c->get_uid();
$uid = $uidGet['uid'];

include_once("functions.php");

$since_id = (int)$_GET['since'] ? (int)$_GET['since'] : 0;
$max_id = (int)$_GET['max'] ? (int)$_GET['max'] : 0;
$page = (int)$_GET['page'] ? (int)$_GET['page'] : 1;
$count = (int)$_GET['count'] ? (int)$_GET['count'] : 10;

if(trim($_GET['name'])){
	$uidUserInfo = $c->show_user_by_name(trim($_GET['name']));
	$isUid = false;
	if($uidUserInfo["error_code"]){
		$uidUserInfo = $c->show_user_by_id(trim($_GET['name']));
		$isUid = true;
	}
}

if(isset($_GET['name']) && !$_GET['friendships']){
	if(!trim($_GET['name'])){
		header("Location: /");
	}
	if(!$isUid){
		$ptl = $c->user_timeline_by_name(trim($_GET['name']), $page, $count, $since_id, $max_id);
	}
	else{
		$ptl = $c->user_timeline_by_id(trim($_GET['name']), $page, $count, $since_id, $max_id);
	}
}
else if(isset($_GET['trend'])){
	if(!trim($_GET['trend'])){
		header("Location: /");
	}
	$ptl = $c->get_trends_statuses(trim($_GET['trend']), $page, $count);
}
else if($_GET['type'] == "bilateral"){
	$ptl = $c->bilateral_timeline($page, $count, $since_id, $max_id);
}
else if($_GET['type'] == "original"){
	$ptl = $c->home_timeline($page, $count, $since_id, $max_id, 0, 1);
}
else if($_GET['type'] == "picture"){
	$ptl = $c->home_timeline($page, $count, $since_id, $max_id, 0, 2);
}
else if($_GET['type'] == "video"){
	$ptl = $c->home_timeline($page, $count, $since_id, $max_id, 0, 3);
}
else if($_GET['type'] == "music"){
	$ptl = $c->home_timeline($page, $count, $since_id, $max_id, 0, 4);
}
else if($_GET['type'] == "favorites"){
	$ptl = $c->get_favorites($page, $count);
}
else if($_GET['type'] == "mentions"){
	$c->reset_count('mention_status');
	$ptl = $c->mentions($page, $count, $since_id, $max_id);
}
else if($_GET['type'] == "public"){
	$ptl = $c->public_timeline();
}
else if(isset($_GET['search'])){
	if(!trim($_GET['search'])){
		header("Location: /");
	}
	$ptl = searchTimeline(urlencode(trim($_GET['search'])), 10, $page, $max_id);
}
else{
	$ptl = $c->home_timeline($page, $count, $since_id, $max_id);
}

if($_GET["post"]){
	$thistimeline = $c->show_status($_GET['post']);
	if($thistimeline["error_code"] == "20101"){
		$thistimeline = getCache($_GET['post']);
		$cachedPost = true;
	}
}
if(!$uid){
	exit;
}
else if(!isset($_GET["cache"]) && ($_GET["name"] && !$uidUserInfo["id"] || $_GET["post"] && !$thistimeline["user"]["id"] || $_GET["friendships"] && $_GET["friendships"] != "followers" && $_GET["friendships"] != "friends")){
	header("Location: /");
}
?>